﻿using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using ASmile.Win.Extend;

namespace ASmile.Win.Controls
{
    public class TreeViewEx : TreeView
    {
        public TreeViewEx()
        {
            Init();
        }

        bool _AllowDropNode;

        void Init()
        {
            HideSelection = false;
            //自已绘制  
            //DrawMode = TreeViewDrawMode.OwnerDrawText;
        }

        /// <summary>
        /// 是否允许拖动节点
        /// </summary>
        [Description("是否允许拖动改变节点位置"), Browsable(true)]
        public bool AllowDropNode
        {
            set
            {
                _AllowDropNode = value;
                if (value) AllowDrop = true;
            }
            get
            {
                return _AllowDropNode;
            }
        }

        /// <summary>
        /// 节点直接拖动完整之后触发
        /// </summary>
        [Description("节点直接拖动完成之后触发"), Browsable(true)]
        public event DropNodeEventHandle DropNodeAfter;

        /// <summary>
        /// 节点直接拖动完整之后触发，返回false取消拖动
        /// </summary>
        [Description("节点拖动之前之后触发，返回false取消拖动"), Browsable(true)]
        public event DropNodeEventHandle DropNodeBefore;

        /// <summary>
        /// 当复选框选择时是否单选
        /// </summary>
        [Description("复选框选是否为单选"), Browsable(true)]
        public bool IsSingleChecked { set; get; }

        protected override void OnItemDrag(ItemDragEventArgs e)
        {
            base.OnItemDrag(e);

            if (!AllowDropNode) return;

            if (e.Button == MouseButtons.Left)
            {
                DoDragDrop(e.Item, DragDropEffects.Move);
            }
        }

        protected override void OnDragEnter(DragEventArgs drgevent)
        {
            base.OnDragEnter(drgevent);

            if (!AllowDropNode) return;

            if (drgevent.Data.GetDataPresent(typeof(TreeNode)))
            {
                drgevent.Effect = DragDropEffects.Move;
            }
            else
            {
                drgevent.Effect = DragDropEffects.None;
            }
        }

        protected override void OnDragDrop(DragEventArgs drgevent)
        {
            base.OnDragDrop(drgevent);

            if (!AllowDropNode) return;

            //获得拖放中的节点  
            TreeNode moveNode = (TreeNode)drgevent.Data.GetData(typeof(TreeNode));
            //根据鼠标坐标确定要移动到的目标节点  
            Point pt;
            TreeNode targetNode;
            pt = PointToClient(new Point(drgevent.X, drgevent.Y));
            targetNode = GetNodeAt(pt);

            if (moveNode == targetNode) return;

            if (DropNodeBefore?.Invoke(moveNode, targetNode) == false)
            {
                return;
            }

            //添加结点
            TreeNode newMoveNode = (TreeNode)moveNode.Clone();

            //if (moveNode.Parent == targetNode.Parent && MessageBox.Show(
            //        $"是否移动到 {targetNode.Text} 的子节点?",
            //        "拖动确认",
            //        MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1) != DialogResult.Yes)
            //{
            //    targetNode.Parent.Nodes.Insert(targetNode.Index, newMoveNode);
            //}

            //else
            //{

            //}
            targetNode.Nodes.Insert(targetNode.Nodes.Count, newMoveNode);
            //展开目标节点,便于显示拖放效果  
            targetNode.Expand();
            //更新当前拖动的节点选择  
            SelectedNode = newMoveNode;

            //移除拖放的节点
            moveNode.Remove();

            DropNodeAfter?.Invoke(newMoveNode, targetNode);
        }

        protected override void OnDrawNode(DrawTreeNodeEventArgs e)
        {
            e.DrawDefault = true;
            base.OnDrawNode(e);
        }

        protected override void OnAfterCheck(TreeViewEventArgs e)
        {
            base.OnAfterCheck(e);

            if (!IsSingleChecked) return;

            if (e.Action == TreeViewAction.ByMouse || e.Action == TreeViewAction.ByKeyboard)
            {
                if (e.Node.Checked)
                {
                    this.LoopTreeNode(LoopNode);
                }
            }

            bool LoopNode(TreeNode node)
            {
                if (node.Name != e.Node.Name)
                {
                    node.Checked = false;
                }
                return false;
            }
        }

        /// <summary>
        /// 根据节点名称找到节点(包含子节点)，如果存在多个则返回首个
        /// </summary>
        public TreeNode FindNode(string name)
        {
            var nodes = Nodes.Find(name, true);
            if (nodes != null && nodes.Count() > 0)
            {
                return nodes.First();
            }
            return null;
        }
        /// <summary>
        /// 根据指定名称选中节点，并返回选中节点
        /// </summary>
        public TreeNode SetSelectedNode(string name, bool isExpand = true)
        {
            var node = FindNode(name);
            if (node != null)
            {
                SelectedNode = node;
                if (isExpand)
                {
                    node.Expand();
                }
            }
            return node;
        }

        //public void InitTree<T>(Action<TreeViewEx,T> OnAdd)
    }

    public delegate bool DropNodeEventHandle(TreeNode moveNode, TreeNode targetNode);

}